package mornd.bootapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author: mornd
 * @dateTime: 2023/5/15 - 16:35
 */

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(App.class, args);
        B1 bean = run.getBean(B1.class);
        System.out.println(run.getBean(B2.class) == run.getBean(B2.class));
        run.close();
    }

    @Configuration
    static class Config {

        @Bean @Scope("prototype")
        public B2 b2() {
            return new B2();
        }
    }
    @Component
    static class B1 implements AutoCloseable {

        @Override
        public void close() throws Exception {
            System.out.println("close...");
        }
    }

    static class B2 {
        // 自动调用关闭方法
//        public void close() {
//            System.out.println("B2 close...");
//        }

        public void shutdown() {
            System.out.println("B2 shutdown");
        }
    }
}
